package com.java110.mall.system.cmd.initPlatformData;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.goodsClass.GoodsClassDto;
import com.java110.dto.housekeeping.HousekeepingTypeDto;
import com.java110.dto.information.InformationDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.system.SystemInfoDto;
import com.java110.intf.goods.IHousekeepingTypeInnerServiceSMO;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.system.IInformationV1InnerServiceSMO;
import com.java110.intf.system.ISystemInfoV1InnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Java110Cmd(serviceCode = "system.initPlatformData")
public class InitPlatformDataCmd extends Cmd {

    public static final int DEFAULT_ROWS = 6;
    public static final int DEFAULT_PAGE = 1;

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private ISystemInfoV1InnerServiceSMO systemInfoV1InnerServiceSMOImpl;

    @Autowired
    private IInformationV1InnerServiceSMO informationV1InnerServiceSMOImpl;

    @Autowired
    private IHousekeepingTypeInnerServiceSMO housekeepingTypeInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        //Assert.hasKeyAndValue(reqJson, "type", "系统错误");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        ResultVo resultVo;
        Map<String, Object> res = new HashMap();
        SystemInfoDto systemInfoDto = BeanConvertUtil.covertBean(reqJson, SystemInfoDto.class);
        int count = systemInfoV1InnerServiceSMOImpl.querySystemInfosCount(systemInfoDto);
        List<SystemInfoDto> systemInfoDtos = null;
        if (count > 0) {
            systemInfoDtos = systemInfoV1InnerServiceSMOImpl.querySystemInfos(systemInfoDto);
            res.put("platformName", systemInfoDtos.get(0).getSystemTitle());
        }else{
            res.put("platformName", "HC商城系统");
        }
        HousekeepingTypeDto housekeepingTypeDto = new HousekeepingTypeDto();
        housekeepingTypeDto.setIsShow("Y");
        housekeepingTypeDto.setShopId("9999");
        List<HousekeepingTypeDto> housekeepingTypeDtos = housekeepingTypeInnerServiceSMOImpl.queryHousekeepingTypes(housekeepingTypeDto);
        freshShopImg(housekeepingTypeDtos);
        res.put("platformProductClass", housekeepingTypeDtos);
        for (HousekeepingTypeDto housekeepingTypeDto1 : housekeepingTypeDtos) {//每个大类下面找不超过6个商品
            ProductDto productDto = new ProductDto();
            productDto.setHktId(housekeepingTypeDto1.getHktId());
            productDto.setRow(DEFAULT_ROWS);
            productDto.setPage(DEFAULT_PAGE);
            productDto.setState(ProductDto.STATE_ON);
            List<ProductDto> productDtos = productInnerServiceSMOImpl.queryProducts(productDto);
            if (productDtos.size() > 0) {
                freshProductDtos(productDtos, productDto);
                housekeepingTypeDto1.setProducts(productDtos);
            }
        }

        InformationDto informationDto = new InformationDto();
        informationDto.setRow(DEFAULT_ROWS);
        informationDto.setPage(DEFAULT_PAGE);
        informationDto.setIsOther("0");
        List<InformationDto> informationDtos = informationV1InnerServiceSMOImpl.queryInformations(informationDto);
        if (CollectionUtils.isNotEmpty(informationDtos)) {
            res.put("platformInformation", informationDtos);
        }
        resultVo = new ResultVo(res);
        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }

    private void freshProductDtos(List<ProductDto> productDtos, ProductDto productDto) {
        if (productDtos == null || productDtos.size() < 1) {
            return;
        }
        //输入
        List<String> productIds = new ArrayList<>();
        for (ProductDto tmpProductDto : productDtos) {
            productIds.add(tmpProductDto.getProductId());
        }
        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (ProductDto tmpProductDto : productDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    url = tmpFileRelDto.getFileRealName();
                    tmpProductDto.setCoverPhoto(url);
                }
            }
        }
    }

    private void findChild(List<GoodsClassDto> goodsClassDtos, GoodsClassDto goodsClass) {
        List<GoodsClassDto> childs = new ArrayList<>();
        for (GoodsClassDto goodsClassDto : goodsClassDtos) {
            if (goodsClassDto.getClassPid().equals(goodsClass.getClassId())) {
                childs.add(goodsClassDto);
            }
        }
        if (childs.size() < 1) {
            return;
        }
        goodsClass.setChildren(childs);
        for (GoodsClassDto goodsClassDto : childs) {
            findChild(goodsClassDtos, goodsClassDto);

        }
    }

    /**
     * @param housekeepingTypeDtos
     */
    private void freshShopImg(List<HousekeepingTypeDto> housekeepingTypeDtos) {
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");

        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");

        for (HousekeepingTypeDto housekeepingTypeDto : housekeepingTypeDtos) {
            if (housekeepingTypeDto.getHktIcon().startsWith("http")) {
                continue;
            }
            housekeepingTypeDto.setHktIcon(imgUrl + housekeepingTypeDto.getHktIcon());
        }
    }


}
